package com.kuang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    //查询数据库的所有信息
    @GetMapping("/userList")
    public List<Map<String,Object>> list(){
        String sql = "select * from user";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }

    @GetMapping("/addUser")
    public String addUser(){
        String sql ="insert into user(id,name,pwd) values(6,'小A',123456)";
        jdbcTemplate.update(sql);
        return "插入成功";
    }

    @GetMapping("/updateUser")
    public String updateUser(){
        String sql ="update user set name = '修改后',pwd=123456 where id=6;";
        jdbcTemplate.update(sql);
        return "修改成功";
    }

    @GetMapping("/delUser")
    public String delUser(){
        String sql ="delete from user where id=6";
        try {
            jdbcTemplate.update(sql);
            return "删除成功";
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return "删除失败";
    }
}
